package com.gexin.core.web;

import com.alibaba.fastjson.JSONObject;
import com.gexin.common.GeXinSessionKit;
import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.interceptor.XAppSafeInterceptor;
import com.gexin.model.weixin.WeixinAppConfig;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.wxaapp.WxaConfig;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * APP 基本的Controller
 *
 * @author 肖得明
 */
@Before(XAppSafeInterceptor.class)//统一错误拦截
public class XAppController extends Controller {


	@NotAction
	public WxaConfig getWxaConfig() {
		return WeixinAppConfig.dao.getWxaConfigs();
	}

	@NotAction
	public void success(boolean state, String data) {
		renderJson(new ApiResponse(true, "success", data));
	}


	@NotAction
	public String getWxappId() {
		String appid = getHeader("wxa_appid");
		if (StringUtil.isBlank(appid)) {
			return "wx588bb29f2791d85a";
		} else {
			return appid;
		}
	}



	/**
	 * 是否是私有的小程序
	 *
	 * @return
	 */
	public Boolean isSingleApp() {
		String type = getHeader("app_type");  //小程序类型  single(单个小程序)   multi(多个小程序)
		if (type == null || type.equals("platform")) {
			return false;
		} else {
			return true;
		}
	}

	@NotAction
	public int getUserId() {
		return getParaToInt("user_id");
	}

	/**
	 * 小程序post提交请求方式接口后台需从流中获取参数
	 *
	 * @return
	 */
	@NotAction
	public JSONObject getRequestData() {
		HttpServletRequest request = getRequest();
		try {
			//获取文件流
			InputStream inStream = request.getInputStream();
			ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = inStream.read(buffer)) != -1) {
				outSteam.write(buffer, 0, len);
			}
			//转换格式
			String result = new String(outSteam.toByteArray(), "utf-8");
			JSONObject json = JSONObject.parseObject(result);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONObject();
		}
	}


	//活动当前登录用户的openID  sessionId换openID
	@NotAction
	public String getAuthOpenid() {
		String sessionId = getAttr("user-token");
		String sessionJson = GeXinSessionKit.getSession(sessionId);
		ApiResult sessionResult = ApiResult.create(sessionJson);
		String openid = sessionResult.get("openid");
		return openid;
	}

	/**
	 * 返回当前登录用户的信息
	 *
	 * @return
	 */
	@NotAction
	public WeixinUser getAuthUser() {
		return WeixinUser.dao.getUserByOpentId(getAuthOpenid());
	}


	@NotAction
	public void success() {
		renderJson(new ApiResponse(true));
	}

	@NotAction
	public void success(Object data) {
		renderJson(new ApiResponse(data));
	}

	@NotAction
	public void success(String msg) {
		renderJson(new ApiResponse(true, msg));
	}

	@NotAction
	public void fail() {
		renderJson(new ApiResponse(false));
	}

	@NotAction
	public void fail(String msg) {
		renderJson(new ApiResponse(false, msg));
	}

	@NotAction
	public void fail(Object obj) {
		renderJson(new ApiResponse(false, "error", obj));
	}

	/**
	 * 获取页码 默认从0开始
	 *
	 * @return
	 */
	@NotAction
	public int getPageIndex() {
		return getParaToInt("page", 0);
	}

	/**
	 * 获取页码 默认从0开始
	 *
	 * @return
	 */
	@NotAction
	public int getpage() {
		return getParaToInt("page", 1);
	}

	/**
	 * 获取每页显示大小
	 *
	 * @return
	 */
	@NotAction
	public int getPageSize() {
		return getParaToInt("size", 10);
	}

	/**
	 * 空数组 没数据返回
	 *
	 * @return
	 */
	@NotAction
	public List<Object> getEmptySz() {
		List<Object> list = new ArrayList<Object>();
		return list;
	}
}
